package cn.legend.travel.comment.service.impl;

import cn.legend.travel.comment.dao.persist.repository.CommentRepository;
import cn.legend.travel.comment.pojo.entity.*;
import cn.legend.travel.comment.pojo.param.InsertCommentParam;
import cn.legend.travel.comment.pojo.vo.CommentListItemVO;
import cn.legend.travel.comment.pojo.vo.SecondCommentVo;
import cn.legend.travel.comment.service.CommentService;
import cn.legend.travel.common.enumerator.ServiceCode;
import cn.legend.travel.common.exception.ServiceException;
import cn.legend.travel.common.pojo.authentication.CurrentPrincipal;
import cn.legend.travel.common.pojo.vo.PageData;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentRepository commentRepository;
    @Value("${travel.dao.default-query-page-size}")
    private Integer pageSize;

    @Override
    public PageData<CommentListItemVO> listByArticle(Long articleId, Integer pageNum) {
        return commentRepository.listByArticle(articleId,pageNum,pageSize);
    }

    @Override
    public PageData<CommentListItemVO> listByDelicacy(Long delicacyId, Integer pageNum) {
        return commentRepository.listByDelicacy(delicacyId,pageNum,pageSize);
    }

    @Override
    public PageData<CommentListItemVO> listByDestination(Long destinationId, Integer pageNum) {
        return commentRepository.listByDestination(destinationId,pageNum,pageSize);
    }

    @Override
    public PageData<CommentListItemVO> listByHotel(Long hotelId, Integer pageNum) {
        return commentRepository.listByHotel(hotelId,pageNum,pageSize);
    }

    @Override
    public PageData<CommentListItemVO> listByProduct(Long productId, Integer pageNum) {
        return commentRepository.listByProduct(productId,pageNum,pageSize);
    }

    @Override
    public PageData<CommentListItemVO> listByReview(Long reviewId, Integer pageNum) {
        return commentRepository.listByReview(reviewId,pageNum,pageSize);
    }

    @Override
    public PageData<CommentListItemVO> listByScenery(Long sceneryId, Integer pageNum) {
        return commentRepository.listByScenery(sceneryId,pageNum,pageSize);
    }

    @Override
    public void insertProductComment(CurrentPrincipal currentPrincipal, InsertCommentParam insertCommentParam) {
        ProductComment reviewComment = new ProductComment();
        InsertCommentParam comment = insertComment(currentPrincipal, insertCommentParam);
        BeanUtils.copyProperties(comment,reviewComment);
        int i = commentRepository.insertProductComment(reviewComment);
        if (i!=1){
            throw new  ServiceException(ServiceCode.ERROR_UNKNOWN,"服务器忙，请稍后再试");
        }
    }

    @Override
    public void insertDelicacyComment(CurrentPrincipal currentPrincipal, InsertCommentParam insertCommentParam) {
        DelicacyComment reviewComment = new DelicacyComment();
        InsertCommentParam comment = insertComment(currentPrincipal, insertCommentParam);
        BeanUtils.copyProperties(comment,reviewComment);
        int i = commentRepository.insertDelicacyComment(reviewComment);
        if (i!=1){
            throw new  ServiceException(ServiceCode.ERROR_UNKNOWN,"服务器忙，请稍后再试");
        }
    }

    @Override
    public void insertDestinationComment(CurrentPrincipal currentPrincipal, InsertCommentParam insertCommentParam) {
        DestinationComment reviewComment = new DestinationComment();
        InsertCommentParam comment = insertComment(currentPrincipal, insertCommentParam);
        BeanUtils.copyProperties(comment,reviewComment);
        int i = commentRepository.insertDestinationComment(reviewComment);
        if (i!=1){
            throw new  ServiceException(ServiceCode.ERROR_UNKNOWN,"服务器忙，请稍后再试");
        }
    }

    @Override
    public void insertGuideComment(CurrentPrincipal currentPrincipal, InsertCommentParam insertCommentParam) {
        GuideComment reviewComment = new GuideComment();
        InsertCommentParam comment = insertComment(currentPrincipal, insertCommentParam);
        BeanUtils.copyProperties(comment,reviewComment);
        int i = commentRepository.insertGuideComment(reviewComment);
        if (i!=1){
            throw new  ServiceException(ServiceCode.ERROR_UNKNOWN,"服务器忙，请稍后再试");
        }
    }

    @Override
    public void insertHotelComment(CurrentPrincipal currentPrincipal, InsertCommentParam insertCommentParam) {
        HotelComment reviewComment = new HotelComment();
        InsertCommentParam comment = insertComment(currentPrincipal, insertCommentParam);
        BeanUtils.copyProperties(comment,reviewComment);
        int i = commentRepository.insertHotelComment(reviewComment);
        if (i!=1){
            throw new  ServiceException(ServiceCode.ERROR_UNKNOWN,"服务器忙，请稍后再试");
        }
    }

    @Override
    public void insertReviewComment(CurrentPrincipal currentPrincipal, InsertCommentParam insertCommentParam) {
        ReviewComment reviewComment = new ReviewComment();
        InsertCommentParam comment = insertComment(currentPrincipal, insertCommentParam);
        BeanUtils.copyProperties(comment,reviewComment);
        int i = commentRepository.insertReviewComment(reviewComment);
        if (i!=1){
            throw new  ServiceException(ServiceCode.ERROR_UNKNOWN,"服务器忙，请稍后再试");
        }
    }

    @Override
    public void insertSceneryComment(CurrentPrincipal currentPrincipal, InsertCommentParam insertCommentParam) {
        SceneryComment sceneryComment = new SceneryComment();
        InsertCommentParam comment = insertComment(currentPrincipal, insertCommentParam);
        BeanUtils.copyProperties(comment,sceneryComment);
        int i = commentRepository.insertSceneryComment(sceneryComment);
        if (i!=1){
            throw new  ServiceException(ServiceCode.ERROR_UNKNOWN,"服务器忙，请稍后再试");
        }
    }

    @Override
    public PageData<SecondCommentVo> getProductSecondComments(Long commentId, Integer pageNum) {
        return commentRepository.getProductSecondComments(commentId,pageNum,pageSize);
    }

    @Override
    public PageData<SecondCommentVo> getScenerySecondComments(Long commentId, Integer pageNum) {
        return commentRepository.getScenerySecondComments(commentId,pageNum,pageSize);
    }

    @Override
    public PageData<SecondCommentVo> getReviewSecondComments(Long commentId, Integer pageNum) {
        return commentRepository.getReviewSecondComments(commentId,pageNum,pageSize);
    }

    @Override
    public PageData<SecondCommentVo> getHotelSecondComments(Long commentId, Integer pageNum) {
        return commentRepository.getHotelSecondComments(commentId,pageNum,pageSize);
    }

    @Override
    public PageData<SecondCommentVo> getDelicacySecondComments(Long commentId, Integer pageNum) {
        return commentRepository.getDelicacySecondComments(commentId,pageNum,pageSize);
    }

    @Override
    public PageData<SecondCommentVo> getGuideSecondComments(Long commentId, Integer pageNum) {
        return commentRepository.getGuideSecondComments(commentId,pageNum,pageSize);
    }

    @Override
    public PageData<SecondCommentVo> getDestinationSecondComments(Long commentId, Integer pageNum) {
        return commentRepository.getDestinationSecondComments(commentId,pageNum,pageSize);
    }

    @Override
    public void deleteDestinationComments(Long commentId) {
       DestinationComment destinationComment = commentRepository.getDestinationSecondComment(commentId);
    }

    @Override
    public void deleteProductSecondComments(Long commentId) {
        ProductComment productComment = commentRepository.getProductSecondComment(commentId);
    }

    @Override
    public void deleteScenerySecondComments(Long commentId) {
       SceneryComment sceneryComment = commentRepository.getScenerySecondComment(commentId);
    }

    @Override
    public void deleteReviewSecondComments(Long commentId) {
       ReviewComment reviewComment = commentRepository.getReviewSecondComment(commentId);
    }

    @Override
    public void deleteHotelSecondComments(Long commentId) {
       HotelComment hotelComment = commentRepository.getHotelSecondComment(commentId);
    }

    @Override
    public void deleteDelicacySecondComments(Long commentId) {
       DelicacyComment delicacyComment = commentRepository.getDelicacySecondComment(commentId);
    }

    @Override
    public void deleteGuideSecondComments(Long commentId) {
      GuideComment guideComment =  commentRepository.getGuideSecondComment(commentId);
    }

    private InsertCommentParam insertComment(CurrentPrincipal currentPrincipal, InsertCommentParam insertCommentParam){
        Long id = currentPrincipal.getId();
        if (id == null){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"用户不存在");
        }
        insertCommentParam.setUserId(id);
        if (insertCommentParam.getPid() == null){
            insertCommentParam.setPid(0L);
        }
        return insertCommentParam;
    }
}
